﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XLB.ExtensionMethod
{
    /// <summary>
    /// 地图操作
    /// </summary>
    public class MapHelper
    {
        #region 电子围栏转换相关
        /// <summary>
        /// 电子围栏转换
        /// </summary>
        /// <param name="ElectronicFence"></param>
        /// <remarks>将 [[104.404962, 36.389787],[104.389128, 36.481404]] 转换为 [{"lng":99.963798,"lat":31.053099},{"lng":99.897985,"lat":31.166259}]</remarks>
        /// <returns></returns>
        public static string ElectronicFenceConvert(string ElectronicFence)
        {
            List<BDElectronicFenceDto> bdlist = new List<BDElectronicFenceDto>();
            var gdlist = JsonConvert.DeserializeObject<object[]>(ElectronicFence);

            foreach (var gd in gdlist)
            {
                var lng = ((Newtonsoft.Json.Linq.JContainer)gd).First;
                var lat = ((Newtonsoft.Json.Linq.JContainer)gd).Last;
                bdlist.Add(new BDElectronicFenceDto { lng = lng.ToString(), lat = lat.ToString() });
            }

            return JsonConvert.SerializeObject(bdlist);
        }

        /// <summary>
        /// 经纬度
        /// </summary>
        private class BDElectronicFenceDto
        {
            public string lng { get; set; }
            public string lat { get; set; }
        }

        #endregion
    }
}
